// pages/show/show.js
var app=new getApp();
var socketOpen = false;
var zhuawawa=new Object();
var player;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    devurl: app.globalData.devurl,
    currentTab: 0,
    showctrl:false,
    startBg: app.globalData.devurl +"/images/startbtn.png",
    dstartBg: app.globalData.devurl +"/images/dstarbtn.png",
    is_startBg:true,
    games:[],
    toy:[],
    LivePlayerContext:"",
    playUrl:"",
    game_result_no: false,
    game_result_yes: false,
    game_result_no_recharge: false,
    game_result_yes_recharge:false,
    direction:true,//移动方向
    coinbalance: 0,
    toyprice:0,
    switchindex:1,
  },
  closebtn: function(e){
      this.setData({
        game_result_no: false,
        game_result_yes: false,
        game_result_no_recharge: false,
        game_result_yes_recharge: false
      }) 
  },
  bindChange: function (e) {
    var that = this;
    that.setData({ currentTab: e.detail.current });
  },
  swichNav: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  statrPlay: function (e) {
    var that = this;
    if (this.data.is_startBg){
      this.setData({
        showctrl: true
      })
      var data = {};
      data.cmd = "start_game";
      wx.sendSocketMessage({
        data: JSON.stringify(data),
      })
    }
  },
  historyTo: function (e) {
    wx.navigateTo({
      url: "../history/history"
    })
  },
  leftFn:function(e){
    console.log(e)
    this.data.direction ? this.move(2) : this.move(0);
  },
  upFn: function (e) {
    console.log(e)
    this.data.direction ? this.move(1) : this.move(2);
  },
  rightFn: function (e) {
    console.log(e)
    this.data.direction ? this.move(3) : this.move(1);
  },
  downFn: function (e) {
    console.log(e)
    this.data.direction ? this.move(0) : this.move(3);
  },
  catchfn:function(){
    var data = {};
    data.cmd = "catch";
    wx.sendSocketMessage({
      data: JSON.stringify(data),
    })
  },
  move:function(index){
    var data = {};
    data.cmd = "move";
    data.direction = "" + index + "";
    wx.sendSocketMessage({
      data: JSON.stringify(data),
    })
  },
  viewswitch:function(e){
    console.log(e)
    var that = this;
    this.data.direction = !this.data.direction;
    if (this.data.switchindex==1){
      this.setData({
        playUrl: "rtmp://13300.liveplay.myqcloud.com/live/13300_"+app.globalData.machine_id+"_2",
        switchindex:2
      })
    }else{
      this.setData({
        playUrl: "rtmp://13300.liveplay.myqcloud.com/live/13300_"+app.globalData.machine_id+"_1",
        switchindex:1
      })
    }
    
  },
  /** 
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        console.log(res)
        that.setData({
          clientHeight: res.windowHeight   //设备的高度等于scroll-view内容的高度
        })
      }
    })
    this.controlfn();
    this.socketFn();
  },
  controlfn:function(){
    var _that=this;
    zhuawawa={
      times:null,
      game_state: function (data) {
        if (data.data == "0") {
          _that.setData({
            is_startBg:false
          })
        } else {
          _that.setData({
            is_startBg: true
          })
        }
      },
      user_profile:function(data){
          console.log(data)
          _that.setData({
            coinbalance: parseInt(data.data.coinbalance)
          })
      },
      start_game: function (data) {
          console.log(data);
          _that.setData({
            coinbalance: parseInt(data.coinbalance)
          })
      },
      game_result:function(data){
          console.log(data)
          _that.setData({
            is_startBg: true,
            showctrl: false,
          })
          if (_that.data.coinbalance < _that.data.toyprice){
              if(data.data=="0"){
                _that.setData({
                  game_result_no:false,
                  game_result_yes:false,
                  game_result_no_recharge:true,
                  game_result_yes_recharge:false
                }) 
              }else{
                _that.setData({
                  game_result_no: false,
                  game_result_yes: false,
                  game_result_no_recharge: false,
                  game_result_yes_recharge: true
                }) 
              }
          }else{
            if (data.data == "0") {
              _that.setData({
                game_result_no: true,
                game_result_yes: false,
                game_result_no_recharge: false,
                game_result_yes_recharge: false
              }) 
            } else {
              _that.setData({
                game_result_no: false,
                game_result_yes: true,
                game_result_no_recharge: false,
                game_result_yes_recharge: false
              }) 
            }
          }
      }
    }
  },  
  socketFn:function(){
    wx.connectSocket({
      url: "wss://catchtoy-slave.mxzhuawawa.com",
    })

    wx.onSocketOpen(function (res) {
      socketOpen = true;
      console.log(app.globalData.machine_id)
      var data = {
        cmd: "login",
        uid: app.user.uid,
        machine_id: app.globalData.machine_id,
        sign: app.user.token,
      };
      sendSocketMessage(JSON.stringify(data));
    })

    wx.onSocketMessage(function (res) {
      console.log(JSON.parse(res.data))
      var msg = JSON.parse(res.data);
      var typefn = msg.cmd;
      if (zhuawawa.hasOwnProperty(msg.cmd)) {
        zhuawawa[typefn](msg);
      }
    })

    wx.onSocketError(function () {
      console.log('websocket连接失败！');
    })

    wx.onSocketClose(function (res) {
      console.log('WebSocket 已关闭！')
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that=this;
    this.data.LivePlayerContext = wx.createLivePlayerContext("video_livePlayer");
    this.setData({
      playUrl: "rtmp://13300.liveplay.myqcloud.com/live/13300_"+app.globalData.machine_id+"_1",
    },function(){
      that.data.LivePlayerContext.stop();
      that.data.LivePlayerContext.play();
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    wx.request({
      url: app.globalData.api_url + '/OpenAPI/v1/ToyMachine/getMachineInfo',
      data: {
        machine_id: app.globalData.machine_id
      },
      method: 'GET',
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res.data)
        var res = res.data;
        that.setData({
          games: res.data.games,
          toy: res.data.toy.images,
          toyprice: res.data.toy.price,
        })
        console.log(res)
      },
      fail: function (res) {
        console.log(res.data)
      }
    })
  },
})
function sendSocketMessage(msg) {
  if (socketOpen) {
    wx.sendSocketMessage({
      data: msg,
      success: function (res) {
        console.log(res)
      },
      fail: function (res) {
        console.log(res)
      }
    })
  }
}
